package com.zret.oasys.operation.dept;

/**
 * operation接口下，查询员工方法
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zret.oasys.dbtools.PageData;
import com.zret.oasys.entity.Dept;
import com.zret.oasys.entity.Emp;
import com.zret.oasys.json.entity.GetTableDataJson;

public class GetDepts implements DeptServletOperation {
	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String key = "";
		
		if (request.getParameter("key") != null)
		{
			key = request.getParameter("key");
		}
		System.out.println(key);	
		
		PageData<Dept> pd = null;
		List<Emp> emplist = null;
		List<Dept> deptlist = null;
		
		// 设置json编码
		response.setContentType("application/json;charset=utf-8");
		
		// 获取page参数
		int page = Integer.valueOf(request.getParameter("page"));
		/** 获取pagesize参数*/
		int pagesize = Integer.valueOf(request.getParameter("limit"));
		
		// 获取查询的List
		pd = deptservice.getDeptsByPageForDeptname(page, pagesize, key);
		emplist  = empService.getEmps();
		deptlist = pd.getData();
		for (int i = 0; i < deptlist.size(); i++)
		{
			for (int j = 0; j < emplist.size(); j++)
			{
				if (deptlist.get(i).getManagerid() == emplist.get(j).getEmpid())
				{
					deptlist.get(i).setEmp(emplist.get(j));
				}
			}
		}
		// 创建Gson对象
		Gson gson = new Gson();
		// 创建输出流
		PrintWriter out = response.getWriter();
		// 将查询的部门list转为字符串
		// String jsonString = gson.toJson(list);
		GetTableDataJson<Dept> data = new GetTableDataJson<Dept>(0, "", pd.getTotalCount(), deptlist);
		//将查询到的部门list转为字符串
		String jsonString = gson.toJson(data);
		// 控制台打印测试
		System.out.println(jsonString);
		// 传输给jsp页面
		out.print(jsonString);
		// 释放out
		out.close();
	}
}
